/**
 * 基类js
 */
/**
 *  测试环境------------
 */
// var restUrl = "http://106.14.172.38:8080/xtcx";
// var imageUrl = "http://106.14.172.38:8990/jmrhupload/";
// var linkUrl = "http://106.14.172.38:8990/jmrh";
// var loginUrl = "http://106.14.172.38:8990/jmrh/#/login";
/**
 *  正式环境------------
 */
var baseUrl = window.location.protocol + '//' + window.location.host //本地地址：http://XXX.XXX.XXX.XXX
var restUrl = baseUrl + ":8080/xtcx";
var imageUrl = baseUrl+"/jmrhupload/";
var linkUrl = baseUrl+"/manager";
var loginUrl = baseUrl+"/manager/#/login";
var indexUrl = baseUrl;
/**
 *  本地环境------------
 */
// var restUrl = "http://localhost:8080/xtcx";
// var imageUrl = "http://localhost/jmrhupload/";
// var linkUrl = "http://localhost:9998/manager";
// var loginUrl = "http://localhost:9998/manager/#/login";

function requset(params) {
    var type = params.type ? params.type : 'get';
    var url = restUrl + params.url;
    var data = params.data;
    var contentType = params.contentType ? params.contentType: "application/json;charset=utf-8";
    var headers = params.headers ? params.headers: "";
    $.ajax({
        url: url,
        type: type,
        dataType: 'json',
        contentType: contentType,
        headers: headers,
        data: data,
        success: function (res) {
            if (res.code == 10000) {
                params.sCallback && params.sCallback(res.data)
            } else {
                params.eCallback && params.eCallback(res)
            }
        },
        error: function (res) {
            console.log("***************AJAX ERROR****************")
        }
    });
}

// 获取参数
function getQueryVariable() {
    var url = window.location.search; //获取url中"?"符后的字串
    var paramsJson = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            paramsJson[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return paramsJson;
}

// 判断是否登陆:
function checkLogin(){
    var storage=window.sessionStorage;
    var userInfoLocal = {};
    if (storage.userInfo) {
        userInfoLocal = JSON.parse(storage.userInfo);
    }
    if (userInfoLocal && userInfoLocal.token) {
        return true;
    } else {
        return false;
    }
}
// 获取登录的信息:
function getUserInfo(){
    var storage=window.sessionStorage;
    var userInfoLocal = {};
    if (storage.userInfo) {
        userInfoLocal = JSON.parse(storage.userInfo);
    }
    if (userInfoLocal && userInfoLocal.token) {
        return userInfoLocal;
    } else {
        return null;
    }
}
// 获取登录用户类型:
function getUserType(){
    var user = getUserInfo();
    if (user) {
        return user.userType;
    } else {
        return null;
    }
}

function datetimeFormatLong2String(longTypeDate){
  if (/^(-)?\d{1,10}$/.test(longTypeDate)) {
    longTypeDate = longTypeDate * 1000;
  } else if (/^(-)?\d{1,13}$/.test(longTypeDate)) {
    longTypeDate = longTypeDate * 1;
  }
  var dateObj = new Date(longTypeDate);
  var month = dateObj.getMonth() + 1;
  var day = dateObj.getDate();
  var hours = dateObj.getHours();
  var minutes = dateObj.getMinutes();
  var seconds = dateObj.getSeconds();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' + day + ' ';
  return UnixTimeToDate;
}


// 隐藏发布需求
function hideXq(){
    $("#xq_btn").hide();
    $("#fbxu").hide();
}
// 隐藏发布成果
function hideCg(){
    $("#cg_btn").hide();
    $("#fbcg").hide();
}
// 隐藏对接 和 报名
function hideMeeting(){
    $("#meeting").hide();
}
// 隐藏加入社区
function hideSq(){
    $("#jrsq").hide();
}

var userInfo = null;


function init(callback){
    var urlParam = getQueryVariable();
    var token = urlParam.token;
    if(token){
        getUserInfoByToken(token,callback);
    } else {
        buildUserInfo();
    }
}
// 更新页面展示
function buildUserInfo(){
    var storage=window.sessionStorage;
    var userInfoLocal = {};
    if (storage.userInfo) {
        userInfoLocal = JSON.parse(storage.userInfo);
    }
    if (userInfoLocal && userInfoLocal.token) {
        $("#login_btn").hide();
        $("#user_info_name").text(userInfoLocal.userName);
        $("#user_info").show();
        // 初始化权限
        initAuthority();
    } else {
        $("#login_btn").show();
        $("#user_info").hide();

    }
}
// 通过token获取用户数据
function getUserInfoByToken(token,callback){
    let params = {
        url: "/index/ubyt",
        headers: {token: token},
        sCallback: function (data) {
            getUserInfoCallback(data);
            buildUserInfo();
            callback && callback();
        },
        eCallback: function (res) {
        }
    }
    requset(params);
}
function getUserInfoCallback(data){
    var storage=window.sessionStorage;
    userInfo = {};
    userInfo.token = data.token;
    userInfo.userName = data.userName;
    userInfo.userType = data.userType;
    userInfo.name = data.name;
    userInfo.cellphone = data.cellphone;
    userInfo.email = data.email;
    storage.userInfo = JSON.stringify(userInfo);
}

// 初始化权限
function initAuthority(){
    // 所有管理员，除了登录和进入控制台，其他都不能操作
    if (getUserType() == 0 || getUserType() == 101 || getUserType() == 102 || getUserType() == 103) {
        hideXq();
        hideCg();
        hideMeeting();
        hideSq();
    }
    // 专家/个人 发布需求  发布成果 不能做
    if (getUserType() == 1) {
        hideXq();
        hideCg();
    }
}

$(function () {

    var storage=window.sessionStorage;
    // 进入页面获取token
    var urlParam = getQueryVariable();
    init();



    // 登陆
    $("#login_btn").on("click",function(){
        var toUrl = loginUrl;
        var myUrl = window.location.href;
        //myUrl = myUrl.replace("http://","");
        var url = toUrl+"?callback="+myUrl;
        var encodeurl = encodeURI(url);
        window.location.href = encodeurl;
    })

    //发布需求
    $("#xq_btn").on("click",function(){
        var myUrl = linkUrl + "/#/manage3/demandLibraryEdit"
        window.location.href = myUrl;
    })

    //发布成果
    $("#cg_btn").on("click",function(){
        var myUrl = linkUrl + "/#/manage3/achieveLibraryEdit"
        window.location.href = myUrl;
    })

    //我的工作台
    $("#gzt_btn").on("click",function(){
        var myUrl = linkUrl + "/#/index/dashboard";
        window.location.href = myUrl;
    })

    $("#gzt").on("click",function(){
        var flag = checkLogin();
        if (!flag) {
            $("#login_btn").click();
        } else {
            $("#gzt_btn").click();
        }
    })

    //点击新用户注册
    $('#register').on('click',function () {
        var path = linkUrl + '/#/register';
        console.log(path)
        window.location.href = path;
    })

    //退出
    $("#logout_btn").on("click",function(){
        var myUrl = window.location.href;
        var indexOf = myUrl.indexOf("token");
        myUrl = myUrl.substring(0,indexOf-1);
        window.location.href = myUrl;
        var storage=window.sessionStorage;
        storage.removeItem("userInfo");
        storage.removeItem("user");
    })
})
